<!--
 * @Description: 
 * @Author: chenjiaxin
 * @Date: 2022-04-16 22:15:46
 * @LastEditTime: 2022-04-16 22:21:10
 * @FilePath: \WebSkillExtend\eg14-代码执行时间\index.html
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var twoSum = function (nums, target) {
        // let index1 = "";
        // let index2 = "";
        // for (let i = 0; i < nums.length; i++) {
        //   let num = nums[i];
        //   let num2 = target - num;
        //   let resultIndex = nums.findIndex((ele) => ele === num2);
        //   if (resultIndex != -1 && resultIndex != i) {
        //     index1 = i;
        //     index2 = resultIndex;
        //     break;
        //   }
        // }
        // return [index1, index2];

        let map = new Map();
        for (let i = 0, len = nums.length; i < len; i++) {
          if (map.has(target - nums[i])) {
            return [map.get(target - nums[i]), i];
          } else {
            map.set(nums[i], i);
          }
        }
        return null;
      };
      var nums = [23, 4, 9, 30, 7, 10];
      var target = 39;
      let start = new Date().getTime();
      let result = twoSum(nums, target);
      console.log(result);
      let now = new Date().getTime();
      console.log(now - start);
    </script>
  </body>
</html>
